Customising Mouse Buttons in Linux

I recently bought a 4K monitor, which is great, but one of the things I noticed that annoys me was that when moving the mouse from one side of the screen to the other I have to pick it up and reset it. Once across the mouse matt is not enough to get the cursor from on side to the other. This is the case even with the acceleration of the mouse set to it's highest value. I've eased this issue by binding a script to three of the extra mouse buttons that repositions the cursor to the top left, middle or top right depending on the button pressed. I'll outline the process below

Run the following command in the terminal

xev -event mouse

Click on the area that appears with the buttons you want to use and pay attention to the output in the terminal. All you need is the number of each button you want to use.

Install xbindkeys and xdotool (on debian based systems that would be)

sudo apt-get install xbindkeys
sudo apt-get install xdotool

Create a file in your home directory (the dot is important)

touch .xbindkeysrc

Edit this file with the bash commands you want to run using the example below as a guide. It does the following:

  • moves the mouspointer to the point (200, 200) from the top left of the screen when I press my button 6.
  • moves the mouspointer to the point (3600, 200) from the top left of the screen, which is the top right of the screen on a 4K monitor, when I press my button 7.
  • moves the mouspointer to the point (1900, 200) from the top left of the screen, which is the middle of the screen, when I press my button 2.

Button 6, 7 and 2 correspond to the scroll wheel on my mouse which can be pushed down and to either side.

"xdotool mousemove 200 200"
        m:0x0 + b:6

"xdotool mousemove 3600 200"
        m:0x0 + b:7

"xdotool mousemove 1900 200"
        m:0x0 + b:2

Run the following to kill any existing xbindkeys process running and restart it. Test it to make sure it works.

pkill -f xbindkeys
xbindkeys

You now need to make sure it runs on startup. Every distro is going to be slightly different so you may need to Google it. However, on Mint when you install xbindkeys it is automatically configured to run on startup

Comments